﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BBPA.BLL
{
    //Account=Master+Vice

    //先消费Master
    //Vice不可提取

    //可消费金额=Account-Frozen
    //可提款金额=Master-Frozen


    //添加商品到订单：冻结，删除订单商品：解冻
    //支付：先(按原冻结记录)解冻，后支付
    public partial class Account_Change_Log
    {


        public static CallBak<int> Add(EnumHelper.UserType UserType_, int SupplierOrStoreID_,
            EnumHelper.AccountChangeReason Reason_, int ReasonRowID_, decimal Money_)
        {
            CallBak<int> callBak = new CallBak<int>();
            //以下情况需要先解冻
            if (In_AccountChangeReason(Reason_, new List<EnumHelper.AccountChangeReason>(){ 
                EnumHelper.AccountChangeReason.Agree_ApplyDraw ,
                EnumHelper.AccountChangeReason.Pay_ExpressMoney,
                //EnumHelper.AccountChangeReason.ExpectPay,
                EnumHelper.AccountChangeReason.Success_BuyOrders 
            })) UnFreeze(UserType_, SupplierOrStoreID_, GetUnFreezeReason(Reason_), ReasonRowID_, Money_);
            //

            Account_Change_Log bll = new Account_Change_Log();
            Supplier bllSupplier = new Supplier();
            Store bllStore = new Store();
            Model.Store modelStore = new Model.Store();
            Model.Supplier modelSupplier = new Model.Supplier();
            //
            Model.Account_Change_Log model = new Model.Account_Change_Log();
            model.Reason = (int)Reason_;
            model.ReasonRowID = ReasonRowID_;
            model.Money = Money_;
            EnumHelper.AccountChangeType _ChangeType = EnumHelper.AccountChangeType.Increase;
            switch (UserType_)
            {
                case EnumHelper.UserType.Supplier://供货商
                    #region 原始金额
                    modelSupplier = bllSupplier.GetModel(SupplierOrStoreID_);
                    model.SupplierID = SupplierOrStoreID_;
                    model.AccountBefore = modelSupplier.Account;
                    model.MasterAccountBefore = modelSupplier.MasterAccount;
                    model.ViceAccountBefore = modelSupplier.ViceAccount;
                    model.FrozenAccountBefore = modelSupplier.FrozenAccount;
                    #endregion
                    //求ChangeType
                    if (In_AccountChangeReason(Reason_, new List<EnumHelper.AccountChangeReason>(){ 
                        EnumHelper.AccountChangeReason.Draw,EnumHelper.AccountChangeReason.Agree_ApplyDraw 
                    })) _ChangeType = EnumHelper.AccountChangeType.Decrease;
                    //求Change账户
                    model.MasterAccountChange = model.Money;
                    #region 新的金额
                    //更新Supplier
                    modelSupplier.MasterAccount += model.MasterAccountChange * InDecrease2Int(_ChangeType);
                    modelSupplier.ViceAccount += model.ViceAccountChange * InDecrease2Int(_ChangeType);
                    modelSupplier.FrozenAccount += model.FrozenAccountChange * InDecrease2Int(_ChangeType);
                    modelSupplier.Account = modelSupplier.MasterAccount + modelSupplier.ViceAccount;
                    
                    //
                    model.AccountAfter = modelSupplier.Account;
                    model.MasterAccountAfter = modelSupplier.MasterAccount;
                    model.ViceAccountAfter = modelSupplier.ViceAccount;
                    model.FrozenAccountAfter = modelSupplier.FrozenAccount;
                    #endregion
                    break;
                case EnumHelper.UserType.Store://孕婴店
                    #region 原始金额
                    modelStore = bllStore.GetModel(SupplierOrStoreID_);
                    model.StoreID = SupplierOrStoreID_;
                    model.AccountBefore = modelStore.Account;
                    model.MasterAccountBefore = modelStore.MasterAccount;
                    model.ViceAccountBefore = modelStore.ViceAccount;
                    model.FrozenAccountBefore = modelStore.FrozenAccount;
                    #endregion
                    //求ChangeType
                    if (In_AccountChangeReason(Reason_, new List<EnumHelper.AccountChangeReason>(){ 
                        EnumHelper.AccountChangeReason.Draw , 
                        EnumHelper.AccountChangeReason.Agree_ApplyDraw ,
                        EnumHelper.AccountChangeReason.Success_BuyOrders,
                        EnumHelper.AccountChangeReason.Pay_ExpressMoney,
                    })) _ChangeType = EnumHelper.AccountChangeType.Decrease;
                    else if (In_AccountChangeReason(Reason_, new List<EnumHelper.AccountChangeReason>(){ 
                        EnumHelper.AccountChangeReason.ExpectExpressMoney,
                        EnumHelper.AccountChangeReason.GetMoney,
                        //EnumHelper.AccountChangeReason.ExpectPay,
                        EnumHelper.AccountChangeReason.ApplyDraw
                    })) _ChangeType = EnumHelper.AccountChangeType.Freeze;
                    else if (In_AccountChangeReason(Reason_, new List<EnumHelper.AccountChangeReason>(){ 
                        EnumHelper.AccountChangeReason._Pay_ExpressMoney,
                        EnumHelper.AccountChangeReason.Cancel_Orders,
                        //EnumHelper.AccountChangeReason._Ready_ExpectPay,
                        //EnumHelper.AccountChangeReason.Cancel_BuyOrders,
                        EnumHelper.AccountChangeReason._Success_BuyOrders,
                        EnumHelper.AccountChangeReason._Agree_ApplyDraw,
                        EnumHelper.AccountChangeReason.NotAgree_ApplyDraw
                    }))
                    {
                        _ChangeType = EnumHelper.AccountChangeType.UnFreeze;
                        //求解冻Money。根据冻结记录获得
                        model.Money = bll.GetModelList("Reason=" + (int)GetFreezeReason(Reason_) + " and ReasonRowID=" + ReasonRowID_)[0].Money;
                    }
                    //求Change账户
                    if (In_AccountChangeReason(Reason_, new List<EnumHelper.AccountChangeReason>(){ 
                        EnumHelper.AccountChangeReason.Recharge , 
                        EnumHelper.AccountChangeReason.Draw,
                        EnumHelper.AccountChangeReason.Agree_ApplyDraw 
                    })) model.MasterAccountChange = model.Money;
                    else if (In_AccountChangeReason(Reason_, new List<EnumHelper.AccountChangeReason>(){ 
                        EnumHelper.AccountChangeReason.Gift
                    })) model.ViceAccountChange = model.Money;
                    else if (In_AccountChangeReason(Reason_, new List<EnumHelper.AccountChangeReason>(){ 
                        EnumHelper.AccountChangeReason.ExpectExpressMoney , 
                        EnumHelper.AccountChangeReason._Pay_ExpressMoney,
                        EnumHelper.AccountChangeReason.GetMoney , 
                        EnumHelper.AccountChangeReason.Cancel_Orders,
                        //EnumHelper.AccountChangeReason._Ready_ExpectPay,
                        //EnumHelper.AccountChangeReason.ExpectPay , 
                        //EnumHelper.AccountChangeReason.Cancel_BuyOrders,
                        EnumHelper.AccountChangeReason._Success_BuyOrders,
                        EnumHelper.AccountChangeReason.ApplyDraw,
                        EnumHelper.AccountChangeReason._Agree_ApplyDraw,
                        EnumHelper.AccountChangeReason.NotAgree_ApplyDraw
                    })) model.FrozenAccountChange = model.Money;
                    else if (In_AccountChangeReason(Reason_, new List<EnumHelper.AccountChangeReason>(){ 
                        EnumHelper.AccountChangeReason.Success_BuyOrders,
                        EnumHelper.AccountChangeReason.Pay_ExpressMoney
                    }))
                    {
                        if (model.MasterAccountBefore - model.FrozenAccountBefore >= model.Money) model.MasterAccountChange = model.Money;
                        else if (model.MasterAccountBefore + model.ViceAccountBefore - model.FrozenAccountBefore >= model.Money)
                        {//Master活动金额不够消费，就先扣Master，再扣Vice
                            model.MasterAccountChange = model.MasterAccountBefore;
                            model.ViceAccountChange = model.Money - model.MasterAccountBefore;
                        }
                        else return callBak.Err("余额不足");
                    }
                    #region 新的金额
                    //更新Store
                    modelStore.MasterAccount += model.MasterAccountChange * InDecrease2Int(_ChangeType);
                    modelStore.ViceAccount += model.ViceAccountChange * InDecrease2Int(_ChangeType);
                    modelStore.FrozenAccount += model.FrozenAccountChange * InDecrease2Int(_ChangeType);
                    modelStore.Account = modelStore.MasterAccount + modelStore.ViceAccount;
                    //
                    model.AccountAfter = modelStore.Account;
                    model.MasterAccountAfter = modelStore.MasterAccount;
                    model.ViceAccountAfter = modelStore.ViceAccount;
                    model.FrozenAccountAfter = modelStore.FrozenAccount;
                    #endregion
                    break;
            }
            if (
                (_ChangeType == EnumHelper.AccountChangeType.Decrease || _ChangeType == EnumHelper.AccountChangeType.Freeze )&& 
                model.Money > model.AccountBefore - model.FrozenAccountBefore
                ) return callBak.Err("余额不足");

            switch (UserType_)
            {
                case EnumHelper.UserType.Supplier://供货商
                    bllSupplier.Update(modelSupplier);
                    break;
                case EnumHelper.UserType.Store://孕婴店
                    bllStore.Update(modelStore);
                    break;
            }

            model.Type = (int)_ChangeType;
            model.AccountChange = model.MasterAccountChange + model.ViceAccountChange;
            int LogID_new = bll.Add(model);
            //
            return callBak.Set(LogID_new);
        }
        /// <summary>
        /// 解冻
        /// </summary>
        /// <param name="UserType_"></param>
        /// <param name="SupplierOrStoreID_"></param>
        /// <param name="Reason_"></param>
        /// <param name="ReasonRowID_"></param>
        /// <returns></returns>
        static CallBak<int> UnFreeze(EnumHelper.UserType UserType_, int SupplierOrStoreID_, EnumHelper.AccountChangeReason Reason_, int ReasonRowID_, decimal Money_)
        {
            return Add(UserType_, SupplierOrStoreID_, Reason_, ReasonRowID_, Money_);
        }
        /// <summary>
        /// 通过 账户修改原因，获取解冻原因
        /// </summary>
        /// <param name="AccountChangeReason_"></param>
        /// <returns></returns>
        static EnumHelper.AccountChangeReason GetUnFreezeReason(EnumHelper.AccountChangeReason ChangeReason_)
        {
            switch (ChangeReason_)
            {
                case EnumHelper.AccountChangeReason.Agree_ApplyDraw:
                    return EnumHelper.AccountChangeReason._Agree_ApplyDraw;
                //case EnumHelper.AccountChangeReason.ExpectPay:
                    //return EnumHelper.AccountChangeReason._Ready_ExpectPay;
                case EnumHelper.AccountChangeReason.Pay_ExpressMoney:
                    return EnumHelper.AccountChangeReason._Pay_ExpressMoney;
                default: return EnumHelper.AccountChangeReason._Success_BuyOrders;
            }
           
        }
        /// <summary>
        /// 通过 账户解冻原因，获取冻结原因
        /// </summary>
        /// <param name="Reason_"></param>
        /// <returns></returns>
        static EnumHelper.AccountChangeReason GetFreezeReason(EnumHelper.AccountChangeReason UnFreezeReason_)
        {
            if (In_AccountChangeReason(UnFreezeReason_, new List<EnumHelper.AccountChangeReason>(){ 
                        EnumHelper.AccountChangeReason._Agree_ApplyDraw , 
                        EnumHelper.AccountChangeReason.NotAgree_ApplyDraw
                    })) return EnumHelper.AccountChangeReason.ApplyDraw;
            else if (In_AccountChangeReason(UnFreezeReason_, new List<EnumHelper.AccountChangeReason>(){ 
                        EnumHelper.AccountChangeReason.Cancel_Orders , 
                        //EnumHelper.AccountChangeReason._Ready_ExpectPay, 
                        EnumHelper.AccountChangeReason._Success_BuyOrders
                    })) return EnumHelper.AccountChangeReason.GetMoney;
            else  return EnumHelper.AccountChangeReason.ExpectExpressMoney;
            
        }
        public static bool In_AccountChangeReason(EnumHelper.AccountChangeReason ChangeReason_, List<EnumHelper.AccountChangeReason> listChangeReason_)
        {
            foreach (EnumHelper.AccountChangeReason changeReason in listChangeReason_)
            {
                if (changeReason == ChangeReason_) return true;
            }
            return false;
        }
        static int InDecrease2Int(EnumHelper.AccountChangeType ChangeType)
        {
            if (ChangeType == EnumHelper.AccountChangeType.Increase || ChangeType == EnumHelper.AccountChangeType.Freeze) return 1;
            else return -1;
        }

    }
    
}
